package huu.phong.youtube;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JWindow;

import com.sun.awt.AWTUtilities;

public class Overlay extends JWindow {

	private static final long serialVersionUID = -8865293923498606923L;
	
	private String title = null;
	private Image format = null;
	private String information = null;
	private Timer timer;
	private JLabel labelWait;
	
	public Overlay() {
		super();
		AWTUtilities.setWindowOpaque(this, false);
		timer = new Timer();
		add(labelWait = new JLabel(new ImageIcon(Overlay.class.getResource("/images/a.gif"))));
		labelWait.setVisible(false);
	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		Graphics2D g2 = (Graphics2D) g;
		g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
		g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
		GradientPaint gp = new GradientPaint(180.0f, 280.0f, new Color(255, 255, 255, 255), 250.0f, 380.0f, new Color(255, 255, 0, 0));
		g2.setPaint(gp);
		g2.setColor(new Color(1f, 1f, 1f, 0.8f));
		g2.setFont(YouTube.DEFAULT_FONT.deriveFont(20f));
		if (title != null && Configure.getInstance().displayOverlay) g2.drawString(title, 5, g2.getFontMetrics().getHeight() - g2.getFontMetrics().getDescent());
		if (information != null) g2.drawString(information, 5, getHeight() - 5);
		g2.setComposite(AlphaComposite.SrcOver.derive(0.7f));
		if (format != null) g2.drawImage(format, getWidth() - format.getWidth(null), 0, null);
	}
	
	public void setTitle(String title, Format format){
		this.title = title;
		setFormat(format);
	}
	
	public void setInformation(String info){
		this.information = info;
		repaint();
		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				information = null;
				Overlay.this.repaint();
			}
		}, 5000);
	}
	
	public void setFormat(Format format){
//		this.format = format != null ? new ImageIcon(Overlay.class.getResource("/images/" + format.getImage())).getImage() : null;
		repaint();
	}
	
	public void setWait(boolean wait){
		labelWait.setVisible(wait);
	}
}